<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Feedback.php
 * 反馈页
 * @author  :  Skiychan <dev@skiy.net>
 * @created :  2015/6/11
 * @modified:  
 * @version :  0.0.1
 */

class Feedback extends CI_Controller {
    public function __construct() {
        parent::__construct();
        //文件上传的路径
        $this->load->service("feedback_service");
    }

    /**
     * 反馈页
     */
    public function index() {
        $this->template->view('user/feedback.tpl');
    }

    /**
     * 上传文件
     */
    public function upload_img() {
        $files = $this->feedback_service->upload("file");
        if (! $files) {
            return Common::ajax_return($this->feedback_service);
        }
        echo json_encode($files);
    }

    /**
     * 删除文件
     * @return array
     */
    public function detele_img() {
        $fname = $this->input->post('file_name');
        $path = $this->feedback_service->file_dir.$fname;
        if (file_exists($path) && unlink($path)) {
            return Common::ajax_return(0);
        }
        //不管有没有删除都返回删除成功
        return Common::ajax_return(0);
    }

    /**
     * 添加反馈信息及发送至邮箱
     */
    public function add() {
        $this->load->service('login_service');
        //如果已登陆则添加用户信息
        if ($this->login_service->is_login()) {
            $userinfo[] = 'Uid: '.$this->login_service->get_user_info('id');
            $nickname = $this->login_service->get_user_info('nickname');
            if (! empty($nickname)) $userinfo[] = 'User: '.$nickname;
            $this->feedback_service->userinfo = $userinfo;
        }
        $title = trim($this->input->post('title', TRUE));
        $content = trim($this->input->post('content', TRUE));
        $qq = $this->input->post('qq', TRUE);
        $email = $this->input->post('email', TRUE);
        $file_path = $this->input->post('file_path', TRUE);
        if(is_array($file_path))
        {
            $file_path = implode("|",$file_path);
        }
        $res = $this->feedback_service->add($content, array('qq' => $qq, 'email' => $email), $title, $file_path);
        if (! $res) {
            return Common::ajax_return($this->feedback_service);
        }
        return Common::ajax_return(0);
    }
}